// models/userModel.js
const pool = require('../config/db');
const bcrypt = require('bcrypt');

// 获取用户通过ID
const getUserById = async (id) => {
  const [rows] = await pool.query('SELECT * FROM users WHERE user_id = ?', [id]);
  return rows[0];
};

// 创建新用户
const createUser = async (username, email, password) => {
  const hashedPassword = await bcrypt.hash(password, 10); // 加密密码
  const [result] = await pool.query('INSERT INTO users (username, email, password) VALUES (?, ?, ?)', [username, email, hashedPassword]);
  return result;
};

// 更新用户信息
const updateUser = async (id, username, email, password) => {
  const hashedPassword = await bcrypt.hash(password, 10); // 加密密码
  await pool.query('UPDATE users SET username = ?, email = ?, password = ? WHERE user_id = ?', [username, email, hashedPassword, id]);
};

// 删除用户
const deleteUser = async (id) => {
  await pool.query('DELETE FROM users WHERE user_id = ?', [id]);
};

module.exports = {
  getUserById,
  createUser,
  updateUser,
  deleteUser,
};
